'''
https://leetcode.cn/problems/one-edit-distance/description/
'''


class Solution:
    def isOneEditDistance(self, s: str, t: str) -> bool:
        m, n = len(s), len(t)
        if abs(m - n) > 1:
            return False

        def f(i, j, cur):
            if cur > 1:
                return False
            if i == m and j == n:
                return cur == 1
            if i == m:
                return f(m, j+1, cur+1)
            if j == n:
                return f(i+1, n, cur+1)
            if s[i] == t[j]:
                return f(i + 1, j + 1, cur)
            else:
                return f(i, j + 1, cur + 1) or f(i + 1, j, cur + 1) or f(i + 1, j + 1, cur + 1)

        return f(0, 0, 0)
